package com.typ.member.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @author heran
 * @version 1.0, 2019/1/30 16:32
 */
@Aspect
@Component
public class LogAop {
    @Pointcut("execution(public * com.typ.member.task..*.*(..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "").substring(16);
        try {
            MDC.put("TRACE_ID", uuid);
            Object obj = proceedingJoinPoint.proceed();
            MDC.remove("TRACE_ID");
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
